package cn.store.service.serviceImp;

import cn.store.dao.UserDao;
import cn.store.domain.PageModel;
import cn.store.domain.User;
import cn.store.service.UserService;
import cn.store.utils.BeanFactory;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class UserServiceImp implements UserService {
	UserDao UserDao=(UserDao) BeanFactory.createObject("UserDao");
	
	
	@Override
	public void userRegist(User user) throws SQLException {
		//实现注册功能
		UserDao.userRegist(user);
		
	}

	@Override
	public User userLogin(User user) throws SQLException {
		//此处:可以利用异常在模块之间传递数据

		//select * from user where username=? and password=?
		User uu=UserDao.userLogin(user);
		if(null==uu){
			throw new RuntimeException("密码有误!");
		} else{
			return uu;
		}
	}
	/**
	 * 分页查询用户
	 * */
	@Override
	public PageModel findUsersWithPage(int curNum, Map<String, Object> searchWords) throws SQLException {
		int pageSize = 12;
		int startIndex = ( curNum - 1 ) * pageSize;
		List<User> list = UserDao.findUsersWithPage(searchWords, startIndex, pageSize);
		int totalRecords = UserDao.findTotalRecords(searchWords);
		PageModel pm = new PageModel(curNum,totalRecords,pageSize);
		pm.setList(list);
		return pm;
	}

	@Override
	public User findUserById(String id) throws SQLException {
		return UserDao.findUserById(id);
	}

	@Override
	public void updateUser(User user) throws SQLException {
		UserDao.updateUser(user);
	}

	@Override
	public int deleteUser(String uid) throws SQLException {
		return UserDao.deleteUser(uid);
	}

	@Override
	public User userRegistcheck(String username) throws SQLException {
		User check=UserDao.userRegistcheck(username);
		return check;
	}

}
